# -*- coding: utf-8 -*-

from services import *
from config import DeviceStatus

class OtpService():

	def validate_deviceid(self, deviceid):
		return self.__check_deviceid(deviceid)

	def set_deviceid(self, deviceid):

		self.__set_deviceid(deviceid, 120)

		#return True#self.__set_device_status(deviceid)

	def reset_device(self):

		self.__set_deviceid('reset', 60)

	def get_gateway_status(self, ip_addr):

		key = 'ip:'+str(ip_addr)+':gateway'
		result = RedisManage.check_exists_key(key)

		return result

	def get_pedometer_minute_data(self):

		device = RedisManage.get_object_to_str('current_deviceid')

		sql_pedometer_minute_data = "SELECT deviceid,daykey,hourkey AS `hour`," + \
									"CASE minutekey WHEN 0 THEN '0-10' " + \
													"WHEN 1 THEN '10-20' " + \
													"WHEN 2 THEN '20-30' " + \
													"WHEN 3 THEN '30-40' " + \
													"WHEN 4 THEN '40-50' " + \
													"WHEN 5 THEN '50-60' " + \
									"END AS `minute`  " + \
								",steps, miles, calorie, createdtime AS `time` " + \
							"FROM pedometer_minute_data WHERE steps != 65535 AND deviceid = '%(device)s' limit 0, 5" % {'device':device}

		pedometer_minute_data = self.__get_pedometer_data(device, sql_pedometer_minute_data)
		
		return pedometer_minute_data

	def get_pedometer_daily_data(self):

		device = RedisManage.get_object_to_str('current_deviceid')

		sql_pedometer_daily_data = "SELECT deviceid, daykey, totaltime, totalsteps, totalmileages, totalcalorie, " + \
							"createdtime AS `time` " + \
							"FROM user_pedometer_daily_data " + \
							"WHERE totaltime != 65535 AND deviceid = '%(device)s'" % {'device':device}

		pedometer_daily_data = self.__get_pedometer_data(device, sql_pedometer_daily_data)

		return pedometer_daily_data

	def get_sleep_minute_data(self):

		device = RedisManage.get_object_to_str('current_deviceid')

		sql_sleep_minute_data = "SELECT deviceid, daykey, hourkey AS `hour`, CONCAT(minutekey,'-',minutekey + 10) AS `minute`, action_cnt,createdtime AS `time` " + \
								"FROM sleep_minute_data " + \
								"WHERE deviceid = '%(device)s' limit 0, 5" % {'device':device}

		sleep_minute_data = self.__get_pedometer_data(device, sql_sleep_minute_data)

		return sleep_minute_data

	def get_sleep_daily_data(self):

		device = RedisManage.get_object_to_str('current_deviceid')

		sql_sleep_daily_data = "SELECT deviceid, daykey,CONCAT(sleep_start_hour,':',sleep_start_minute) AS start_sleep,CONCAT(sleep_end_hour,':',sleep_end_minute) AS end_sleep,createdtime AS `time` " + \
								"FROM user_sleep_daily_data " + \
								"WHERE deviceid = '%(device)s'" % {'device':device}

		sleep_daily_data = self.__get_pedometer_data(device, sql_sleep_daily_data)

		return sleep_daily_data

	def __get_pedometer_data(self, device, sql):

		datas = []

		result = dbtool.operateDB('get',sql)

		for row in result:
			row['time'] = str(row['time'])
			datas.append(row)

		return datas

	def get_current_device_status(self):

		current_device = RedisManage.get_object_to_str('current_deviceid')

		if current_device is None:
			return {'deviceid':None, 'device_status':-1}

		sql = "select deviceid, device_status from deviceinfo where deviceid = '%(deviceid)s' " % {'deviceid':current_device}

		result = dbtool.operateDB('get',sql)

		if len(result) == 0:
			return {'deviceid':None, 'device_status':-1}

		return result[0]

	def __set_deviceid(self, deviceid, time = 0):

		return RedisManage.add_object_to_str('transcribe_deviceid', deviceid, time)

	def __check_deviceid(self, device):

		sql = "select device_status from deviceinfo where deviceid = '%(device)s' " % {'device':device}

		result = dbtool.operateDB('get',sql)

		if len(result) == 0:
			return False

		if int(result[0]['device_status']) != DeviceStatus.Available:
			return False
		return True

	def get_device_status(self, page_index, page_size = 50):

		page_index = 0 if page_index < 1 else (page_index - 1)*page_size

		sql_1 = 'SELECT deviceid, device_status FROM deviceinfo ORDER BY device_status DESC LIMIT %(page_index)d, %(page_size)d ' % {'page_index':page_index, 'page_size':page_size}
		sql_2 = 'SELECT COUNT(DISTINCT deviceid) AS cnt FROM deviceinfo'

		result_1 = dbtool.operateDB('get',sql_1)
		result_2 = dbtool.operateDB('get',sql_2)

		total_cnt = int(result_2[0][0])

		pass

	'''
	def __set_device_status(self, device):

		sql = "update deviceinfo set device_status = "+str(DeviceStatus.Using)+", modifiedtime = now() where deviceid = '%(device)s' " % {'device':device}

		result = dbtool.operateDB('update',sql)

		return result


	def __check_deviceid(self, deviceid):
		sql = " SELECT COUNT(*) AS cnt FROM deviceinfo WHERE deviceid = '%(deviceid)s'" % {'deviceid':deviceid}

		result = dbtool.operateDB('get',sql)

		if int(result[0]['cnt']) > 0:
			return self.__check_deviceid_status(deviceid)
		else:
			return False
	'''

	'''
	def get_data(self, device):

		sql_pedometer_minute_data = "SELECT deviceid,daykey,hourkey AS `hour`," + \
									"CASE minutekey WHEN 0 THEN '0-10' " + \
													"WHEN 1 THEN '10-20' " + \
													"WHEN 2 THEN '20-30' " + \
													"WHEN 3 THEN '30-40' " + \
													"WHEN 4 THEN '40-50' " + \
													"WHEN 5 THEN '50-60' " + \
									"END AS `minute`  " + \
								",steps, miles, calorie, createdtime AS `time` " + \
							"FROM pedometer_minute_data WHERE steps != 65535 AND deviceid = '%(device)s'" % {'device':device}

		pedometer_minute_data = self.__get_pedometer_data(device, sql_pedometer_minute_data)
		
		sql_pedometer_daily_data = "SELECT deviceid, daykey, totaltime, totalsteps, totalmileages, totalcalorie, " + \
							"createdtime AS `time` " + \
							"FROM user_pedometer_daily_data " + \
							"WHERE totaltime != 65535 AND deviceid = '%(device)s'" % {'device':device}

		pedometer_daily_data = self.__get_pedometer_data(device, sql_pedometer_daily_data)

		sql_sleep_minute_data = "SELECT deviceid, daykey, hourkey AS `hour`, CONCAT(minutekey,'-',minutekey + 10) AS `minute`, action_cnt,createdtime AS `time` " + \
								"FROM sleep_minute_data " + \
								"WHERE deviceid = '%(device)s'" % {'device':device}

		sleep_minute_data = self.__get_pedometer_data(device, sql_sleep_minute_data)

		sql_sleep_daily_data = "SELECT deviceid, daykey,CONCAT(sleep_start_hour,':',sleep_start_minute) AS start_sleep,CONCAT(sleep_end_hour,':',sleep_end_minute) AS end_sleep,createdtime AS `time` " + \
								"FROM user_sleep_daily_data " + \
								"WHERE deviceid = '%(device)s'" % {'device':device}

		sleep_daily_data = self.__get_pedometer_data(device, sql_sleep_daily_data)

		return sql_pedometer_daily_data
	'''